$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/fyclassteacher/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
			{ label: '班级', name: 'classId', index: 'class_id', width: 80,formatter: function(cellvalue, options, rowdata) {
                    var classname ;
                    $.ajax({
                        type: "POST",
                        async:false,
                        url: baseURL + "sys/fyclasstable/info/"+rowdata.classId,
                        success: function(r){
                            classname = r.fyClassTable.classname;

                        }
                    });
                    return classname;
                } },
			{ label: '教师', name: 'teacherId', index: 'teacher_id', width: 80,formatter: function(cellvalue, options, rowdata) {
                    var teachername ;
                    $.ajax({
                        type: "POST",
                        async:false,
                        url: baseURL + "sys/fyusers/info/"+rowdata.teacherId,
                        success: function(r){
                            teachername = r.fyUsers.userName;

                        }
                    });
                    return teachername;
                } }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    vm.queryClass();
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
            classid:null
		},
		showList: true,
		title: null,
		fyClassTeacher: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.fyClassTeacher = {};
			vm.getClass("add");
			vm.getTeacher("add");
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            vm.getTeacher("update");
            vm.getClass("update");

            vm.getInfo(id);

		},
		saveOrUpdate: function (event) {
			var url = vm.fyClassTeacher.id == null ? "sys/fyclassteacher/save" : "sys/fyclassteacher/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.fyClassTeacher),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/fyclassteacher/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "sys/fyclassteacher/info/"+id, function(r){
                vm.fyClassTeacher = r.fyClassTeacher;
            });
		},
        getClass: function(mn){
            $("#classId").html("");
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/fyclasstable/alllist",
                success: function(r){
                    var temp;
                        $.each(r.alllist, function (i) {
                        if(mn =='add' && i ==0){
                            vm.fyClassTeacher.classId = r.alllist[i].id;
                        }
                        temp +="<option value='"+r.alllist[i].id+"'>"+r.alllist[i].classname+"</option>";

                    });
                    $("#classId").append(temp);

                }
            });
        },
        getTeacher: function(mn){
            $("#teacherId").html("");
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/fyusers/allteacherlist",
                success: function(r){
                    var temp;
                    $.each(r.alllist, function (i) {
                        if(mn =='add' && i ==0){
                            vm.fyClassTeacher.teacherId = r.alllist[i].userId;
                        }
                        temp +="<option value='"+r.alllist[i].userId+"'>"+r.alllist[i].userName+"</option>";

                    });
                    $("#teacherId").append(temp);

                }
            });



        },
        queryClass: function(){
            $("#class_id").html("");
            $.get(baseURL + "sys/fyclasstable/alllist", function(r){
                var temp = "<option value=''></option>";
                $.each(r.alllist, function (i) {

                    temp +="<option value='"+r.alllist[i].id+"'>"+r.alllist[i].classname+"</option>";

                });
                $("#class_id").append(temp);

            });
        },
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'classid': vm.q.classid},
                page:page
            }).trigger("reloadGrid");
		}
	}
});